package com.zjitc.chapter12.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date>{
    private String pattern="yyyy-MM-dd";

    @Override
    public Date convert(String source) {
        // source字符串转日期
        try {
            Date date = new SimpleDateFormat(pattern).parse(source);
            return date;
        } catch (ParseException e) {
            throw new IllegalArgumentException("无效日期格式:"+pattern);
        }

    }
}
